/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * dlSource.java
 *
 * Created on Aug 11, 2010, 4:46:14 PM
 */

package Gui;

import BusinessLogic.BackGroundUtility;
import DTO.Zone;
import BusinessLogic.ZoneManagement;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Administrator
 */
public class DLZone extends javax.swing.JDialog {

    private ZoneManagement zoneMag;
    private boolean flag = false;
    private Vector<Zone> listZone;
    private DefaultTableModel model;
    private int index = 0;

    /** Creates new form dlSource */
    public DLZone(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        //set background
        ImagePanel iPanel = new ImagePanel(BackGroundUtility.getImage());
        getContentPane().add(iPanel);
        //setlocation for application
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension dim = toolkit.getScreenSize();
        Dimension dimApp = getSize();
        int pointX = (dim.width - dimApp.width)/2;
        int pointY = (dim.height - dimApp.height)/2 - 10;
        setLocation(pointX, pointY);

        //Create object zone management to access database
        zoneMag = new ZoneManagement();
        //call method load listZone into table
        loadZone();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnBackground = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtNumberBooth = new javax.swing.JTextField();
        txtHeigth = new javax.swing.JTextField();
        txtMoney = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtID = new javax.swing.JTextField();
        txtZone = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtWidth = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        btnPrev = new javax.swing.JButton();
        btnNext = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbZone = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Zone Management");

        pnBackground.setBackground(new java.awt.Color(0, 102, 102));
        pnBackground.setOpaque(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Zone Management"));
        jPanel2.setLayout(null);

        jLabel4.setText("id");
        jPanel2.add(jLabel4);
        jLabel4.setBounds(50, 30, 40, 14);

        jLabel1.setText("Money");
        jPanel2.add(jLabel1);
        jLabel1.setBounds(50, 150, 40, 14);
        jPanel2.add(txtNumberBooth);
        txtNumberBooth.setBounds(130, 120, 210, 19);
        jPanel2.add(txtHeigth);
        txtHeigth.setBounds(270, 90, 70, 19);
        jPanel2.add(txtMoney);
        txtMoney.setBounds(130, 150, 210, 19);

        jLabel6.setText("Number Booth");
        jPanel2.add(jLabel6);
        jLabel6.setBounds(50, 120, 84, 14);

        txtID.setEditable(false);
        jPanel2.add(txtID);
        txtID.setBounds(130, 30, 90, 19);
        jPanel2.add(txtZone);
        txtZone.setBounds(130, 60, 210, 19);

        jLabel5.setText("H:");
        jPanel2.add(jLabel5);
        jLabel5.setBounds(250, 90, 20, 14);
        jPanel2.add(txtWidth);
        txtWidth.setBounds(150, 90, 70, 19);

        jLabel2.setText("Zone ");
        jPanel2.add(jLabel2);
        jLabel2.setBounds(50, 60, 40, 14);

        jLabel7.setText("Size ");
        jPanel2.add(jLabel7);
        jLabel7.setBounds(50, 90, 40, 14);

        jLabel8.setText("W:");
        jPanel2.add(jLabel8);
        jLabel8.setBounds(130, 90, 20, 14);

        jPanel3.setLayout(null);

        btnPrev.setText("Prev");
        btnPrev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrevActionPerformed(evt);
            }
        });
        jPanel3.add(btnPrev);
        btnPrev.setBounds(50, 10, 70, 23);

        btnNext.setText("Next");
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });
        jPanel3.add(btnNext);
        btnNext.setBounds(140, 10, 70, 23);

        jButton3.setText("First");
        jPanel3.add(jButton3);
        jButton3.setBounds(230, 10, 70, 23);

        jButton4.setText("Last");
        jPanel3.add(jButton4);
        jButton4.setBounds(320, 10, 70, 23);

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        jPanel3.add(btnSave);
        btnSave.setBounds(50, 50, 70, 23);

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        jPanel3.add(btnAdd);
        btnAdd.setBounds(140, 50, 70, 23);

        jButton7.setText("Del");
        jPanel3.add(jButton7);
        jButton7.setBounds(230, 50, 70, 23);

        jButton8.setText("Cancel");
        jPanel3.add(jButton8);
        jButton8.setBounds(320, 50, 70, 23);

        tbZone.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbZone.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbZoneMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(tbZone);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 437, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 437, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 437, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout pnBackgroundLayout = new javax.swing.GroupLayout(pnBackground);
        pnBackground.setLayout(pnBackgroundLayout);
        pnBackgroundLayout.setHorizontalGroup(
            pnBackgroundLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnBackgroundLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnBackgroundLayout.setVerticalGroup(
            pnBackgroundLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnBackgroundLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        getContentPane().add(pnBackground, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnPrevActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrevActionPerformed
        // TODO add your handling code here:
        if(index  > 0){
            //Display a Zone
            displayObject(--index);
        }
}//GEN-LAST:event_btnPrevActionPerformed

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        // TODO add your handling code here:
        if(index + 1 < listZone.size()){
            //Display a Zone
            displayObject(++index);
        }
    }//GEN-LAST:event_btnNextActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:
        //gettext from textfiels
        String id = txtID.getText();
        String name = txtZone.getText();
        float money = Float.parseFloat(txtMoney.getText());
        int numberBooth = Integer.parseInt(txtNumberBooth.getText());
        String size = txtWidth.getText() + "x" + txtHeigth.getText();
        //create a Zone from textfiels
        Zone z = new Zone(id, name, size, numberBooth, money);
        if(flag)
        {
            zoneMag.insert(z);
            flag = false;
        }
        else
        {
            zoneMag.update(z);
            
        }
        //LoadZone again
        loadZone();
}//GEN-LAST:event_btnSaveActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        //call method clearText()
        clearText();
        //method createID automatic create new id
        String id = zoneMag.createID();
        txtID.setText(id);
        flag = true;
}//GEN-LAST:event_btnAddActionPerformed

    private void tbZoneMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbZoneMousePressed
        // TODO add your handling code here:
        index = tbZone.getSelectedRow();
        displayObject(index);
    }//GEN-LAST:event_tbZoneMousePressed

    public void loadZone(){
        //System.out.println("jfalsjfk");
        model = new DefaultTableModel(null, new Object[]{"IDZone", "Name", "Money", "Size", "NumberBooth"});
        tbZone.setModel(model);

        listZone = zoneMag.loadZone();
        for(Zone z : listZone){
            Vector rowData = new Vector();
            rowData.add(z.getIDzone());
            rowData.add(z.getName());
            rowData.add(z.getMoney());
            rowData.add(z.getSize());
            rowData.add(z.getNumberBooth());
            model.addRow(rowData);
        }
        //Display a Zone
        displayObject(index);
    }

    private void displayObject(int index){
        Zone z = null;
        if(listZone.size() > 0){
            z = listZone.get(index);
        } else{
            return;
        }
        //get width, heigth from size
        String w, h, str;
        str = z.getSize();
        int i = str.indexOf("x");
        w = str.substring(0, i);
        h = str.substring(i + 1);
        //set values for textfiels
        txtNumberBooth.setText(""+z.getNumberBooth());
        txtID.setText(""+z.getIDzone());
        txtMoney.setText(""+z.getMoney());
        txtZone.setText(z.getName());
        txtWidth.setText(w);
        txtHeigth.setText(h);
    }

    private void clearText(){
        txtHeigth.setText("");
        txtMoney.setText("");
        txtNumberBooth.setText("");
        txtWidth.setText("");
        txtZone.setText("");
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnPrev;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnBackground;
    private javax.swing.JTable tbZone;
    private javax.swing.JTextField txtHeigth;
    private javax.swing.JTextField txtID;
    private javax.swing.JTextField txtMoney;
    private javax.swing.JTextField txtNumberBooth;
    private javax.swing.JTextField txtWidth;
    private javax.swing.JTextField txtZone;
    // End of variables declaration//GEN-END:variables

}
